package com.xinqi.modules.third.controller;

import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.service.CaptchaService;
import com.anji.captcha.util.StringUtils;
import com.xinqi.common.base.response.R;
import com.xinqi.modules.third.utils.ChapterUtils;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 图形验证码-控制器
 * @author: dzy
 * @date: 2023/8/29 14:48
 */
@Slf4j
@RestController
@RequestMapping("/captcha")
public class SysCaptchaController {

    @Resource
    private CaptchaService captchaService;

    /**
     * 获取图形验证码
     * @author: dzy
     * @date: 2023/8/29 14:48
     */
    @PostMapping("/get-code")
    @Operation(summary = "获得验证码")
    public R<Object> getCode(@RequestBody CaptchaVO data, HttpServletRequest request) {
        assert request.getRemoteHost() != null;
        data.setBrowserInfo(ChapterUtils.getRemoteId(request));
        ResponseModel model = captchaService.get(data);
        return R.ok(model.getRepMsg(), model.getRepData());
    }

    /**
     * 校验图形验证码
     * @author: dzy
     * @date: 2023/8/29 14:48
     */
    @PostMapping("/check-code")
    @Operation(summary = "校验验证码")
    public R<Object> checkCode(@RequestBody CaptchaVO data, HttpServletRequest request) {
        data.setBrowserInfo(ChapterUtils.getRemoteId(request));
        ResponseModel model = captchaService.check(data);
        return R.ok(model);
    }

}
